/**
 * RUtil.java ##copyright## This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any later version. This
 * library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
 * General Public License for more details. You should have received a copy of the GNU Lesser
 * General Public License along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * @author ##author##
 * @modified ##date##
 * @version ##version##
 */
package fiveagon.draw;

import geomerative.RGeomElem;
import geomerative.RPoint;
import geomerative.RShape;

import java.util.Iterator;

import processing.core.PApplet;
import toxi.color.ColorList;

public class RUtil {
  private RUtil() {}

  public static void colorChildren(RShape shp, ColorList colorlist) {
    int shapeFill = colorlist.getRandom().toARGB();
    shp.setFill(shapeFill);

    for (int i = 0; i < shp.countChildren(); i++) {
      int childFill = colorlist.getRandom().toARGB();

      // If everything is on one layer, color everything differently
      // From one layer Illustrator SVG, everything is a child with no
      // children
      if (shp.children[i].countChildren() <= 0) {
        shp.children[i].setFill(childFill);
      } else { // Color everything (j) on the same layer (i) w/ the same color.
        for (int j = 0; j < shp.children[i].countChildren(); j++) {
          shp.children[i].children[j].setFill(childFill);
        }
      }
    }
  }

  public static void removeOffScreen(Iterable<? extends RGeomElem> iterable, PApplet parent) {
    removeOffScreen(iterable, parent.width, parent.height);
  }

  public static void removeOffScreen(Iterable<? extends RGeomElem> iterable, int width, int height) {
    for (Iterator<? extends RGeomElem> i = iterable.iterator(); i.hasNext();) {
      if (isOffScreen(i.next(), width, height)) {
        i.remove();
      }
    }
  }

  public static boolean isOffScreen(RGeomElem shp, PApplet parent) {
    return isOffScreen(shp, parent.width, parent.height);
  }

  public static boolean isOffScreen(RGeomElem shp, int width, int height) {
    RPoint topLeft = shp.getTopLeft();
    RPoint bottomRight = shp.getBottomRight();

    if (topLeft.x > width || topLeft.y > height) {
      return true;
    } else if (bottomRight.x < 0 || bottomRight.y < 0) {
      return true;
    } else {
      return false;
    }
  }
}
